package sitemaps;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;

/**
 * Quick'n dirty sitemaps generator.
 * <p>
 * 
 * @author Adrian (http://eurotrip.com.ar/java-sitemaps/)
 */
public final class Generator {
	
	private static final String SITEMAPS_GENERATOR_XML = "/generator.xml";

	public static void main(final String[] args) throws Exception {
		final Generator generator = (Generator) new ResourceLoader().load(SITEMAPS_GENERATOR_XML);
		if(args != null && args.length > 1 ) {
			generator.setLocation(args[0], args[1]);
		}
		generator.makeSitemap();
	}
	
	public void setLocation(String baseUrl, String basePath) {
		locations.clear();
		locations.add(new SitemapLocation(baseUrl, basePath));
	}

	private final void makeSitemap() throws Exception {

		final SiteParser parser = new SiteParser(
				accepedExtensions, rejectedExtensions, indexes);
		final SitemapBuilder builder = new SitemapBuilder(dateFormat, zone, changefreq, priority);
		final SitemapTransformer transformer = new SitemapTransformer();
		final SitemapNotifier sitemapNotifier = new SitemapNotifier(basePingUrls, encoding);

		if(locations != null)
			for(SitemapLocation location: locations) {
				final Sitemap site = parser.parseLocation(location);
				final Document document = builder.build(site);
				
				transformer.setDocument(document);
				if(outputFilename != null) {
					final String outputFilename = makeOutputPath(location, this.outputFilename);
					transformer.output(templateFilename, outputFilename);
				}
				if(htmlOutputFilename != null) {
					final String htmlOutputFilename = makeOutputPath(location, this.htmlOutputFilename);
					transformer.output(htmlTemplateFilename, htmlOutputFilename);
				}
				//I think this ping isn't working anymore for google.
				if(basePingUrls != null) {
					final String sitemapUrl = makeSitemapUrl(site, outputFilename);
					sitemapNotifier.ping(sitemapUrl);
				}
			}
		
	}

	private String makeSitemapUrl(final Sitemap site, final String outputFilename) {
		return site.getBaseUrl() + outputFilename;
	}

	private String makeOutputPath(SitemapLocation location, String outputFilename) {
		return location.getBasePath() + File.separator + outputFilename;
	}
	
	private List<String> indexes = new ArrayList<String>();
	private List<String> basePingUrls = new ArrayList<String>();
	private List<String> accepedExtensions = new ArrayList<String>();
	private List<String> rejectedExtensions = new ArrayList<String>();
	private List<SitemapLocation> locations = new ArrayList<SitemapLocation>();
	private String zone;
	private String priority;
	private String encoding;
	private String changefreq;
	private String dateFormat;
	private String outputFilename;
	private String templateFilename;
	private String htmlOutputFilename;
	private String htmlTemplateFilename;
	
	public List<SitemapLocation> getLocations() {
		return locations;
	}
	public void setLocations(List<SitemapLocation> locations) {
		this.locations = locations;
	}

	public List<String> getAccepedExtensions() {
		return accepedExtensions;
	}

	public void setAccepedExtensions(List<String> accepedExtensions) {
		this.accepedExtensions = accepedExtensions;
	}

	public String getDateFormat() {
		return dateFormat;
	}

	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}

	public List<String> getIndexes() {
		return indexes;
	}

	public void setIndexes(List<String> indexes) {
		this.indexes = indexes;
	}

	public String getOutputFilename() {
		return outputFilename;
	}

	public void setOutputFilename(String outputFilename) {
		this.outputFilename = outputFilename;
	}

	public List<String> getRejectedExtensions() {
		return rejectedExtensions;
	}

	public void setRejectedExtensions(List<String> rejectedExtensions) {
		this.rejectedExtensions = rejectedExtensions;
	}

	public String getTemplateFilename() {
		return templateFilename;
	}

	public void setTemplateFilename(String templateFilename) {
		this.templateFilename = templateFilename;
	}

	public String getZone() {
		return zone;
	}

	public void setZone(String zone) {
		this.zone = zone;
	}

	public List<String> getBasePingUrls() {
		return basePingUrls;
	}

	public void setBasePingUrls(List<String> basePingUrl) {
		this.basePingUrls = basePingUrl;
	}

	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	public String getHtmlOutputFilename() {
		return htmlOutputFilename;
	}

	public void setHtmlOutputFilename(String htmlOutputFile) {
		this.htmlOutputFilename = htmlOutputFile;
	}

	public String getHtmlTemplateFilename() {
		return htmlTemplateFilename;
	}

	public void setHtmlTemplateFilename(String htmlTemplateFile) {
		this.htmlTemplateFilename = htmlTemplateFile;
	}

	public String getChangefreq() {
		return changefreq;
	}

	public void setChangefreq(String changefreq) {
		this.changefreq = changefreq;
	}

	public String getPriority() {
		return priority;
	}

	public void setPriority(String priority) {
		this.priority = priority;
	}
}
